package com.kuaimacode.kframework.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.util
 * @Description: created by IntelliJ IDEA
 * @date 2019-03-16 21:40
 */
@Slf4j
public final class CommonUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 从base64字符串中获取文件后缀名
     * @param base64Str
     * @return
     */
    public static String getSuffixFromBase64(String base64Str) {
        if (StringUtils.isEmpty(base64Str)) {
            return "";
        }

        try {
            String prefix = base64Str.substring(0, base64Str.indexOf(';'));
            return prefix.substring(prefix.indexOf('/') + 1);
        } catch (Exception e) {
            log.error("从base64字符串获取文件后缀名失败", e);
            return "";
        }
    }



    /**
     * 生成订单号
     *
     * @param pre 订单号前缀
     * @return
     */
    public static String generateOrderNo(String pre, Long userId) {
        String orderNo = "";
        if(pre != null){
            orderNo += pre;
        }
        orderNo += sdf.format(new Date()) + userId + (1 + (int) (Math.random() * 1000));
        return orderNo;
    }

    /**
     * 生成订单唯一识别码
     *
     * @param pre 订单号前缀
     * @return
     */
    public static String generateOrderNum(String pre, Long userId) {
        //生成
        String orderNo = pre + ((int)((Math.random()*9+1)*10000)) + userId + (System.currentTimeMillis() / 1000);
        return orderNo;
    }


    /**
     * 通过经纬度获取距离(单位：米)
     *
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return 距离
     */
    public static double getDistance(double lat1, double lng1, double lat2,
                                     double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }

    /**
     * 本地图片转换Base64的方法
     *
     * @param imgPath     
     */

    public static String imageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            log.error("转换base64数据失败, imgPath: {}", imgPath, e);
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(Objects.requireNonNull(data));
    }

    public static void main(String[] args) {
        //System.out.println(generateOrderNum("HK",530L));
        //System.out.println(generateOrderNo("HK",530L));

        double distance = getDistance(34.2675560000, 108.9534750000,
                34.2464320000, 108.9534750000);
        System.out.println("距离" + distance / 1000 + "公里");

    }

}
